#include <iostream>
#include <fstream>
#include "contacts.pb.h"

int main()
{
    contacts::Contacts cons;
    std::ifstream in("contacts.bin", std::ios::in | std::ios::binary);
    if (!in.is_open())
    {
        std::cout << "open contacts.bin error!" << std::endl;
        return -1;
    }
    else if (!cons.ParsePartialFromIstream(&in))
    {
        std::cout << "反序列化失败!" << std::endl;
        return -1;
    }

    for (int i = 0; i < cons.contacts_size(); i++)
    {
        std::cout << "------------联系人--------------" << std::endl;
        std::cout << "-------------------------------" << std::endl;
        auto people = cons.contacts(i);
        std::cout << "姓名: ";
        std::cout << people.name() << std::endl;
        std::cout << "年龄: " << people.age() << std::endl;
        std::cout << "-----------联系电话-------------" << std::endl;
        for (int i = 0; i < people.phones_size(); i++)
        {
            contacts::PeopleInfo_Phone phone = people.phones(i);
            std::cout << phone.number() << std::endl;
            std::cout << "电话类型: " << phone.PhoneType_Name(phone.type()) << std::endl;
        }
        if (people.has_data())
        {
            std::cout << "------------地址----------------" << std::endl;
            google::protobuf::Any any = people.data();
            contacts::Address address;
            any.UnpackTo(&address);
            std::cout << "工作单位地址: " << address.util_address() << std::endl;
            std::cout << "家庭地址: " << address.home_address() << std::endl;
            std::cout << "--------------------------------" << std::endl;
            std::cout << "----------- 联系人--------------" << std::endl;
        }
        else
        {
            std::cout << "any is not setting!" << std::endl;
        }
        std::cout<<"--------其他联系方式-------"<<std::endl;
        // 查看其他联系方式
        contacts::PeopleInfo::OtherContactCase other_con = people.other_contact_case();
        switch (other_con)
        {
        case contacts::PeopleInfo::OtherContactCase::kQq:
            std::cout << "QQ: " << people.qq() << std::endl;
            break;
        case contacts::PeopleInfo::OtherContactCase::kWechat:
            std::cout << "微信: " << people.wechat() << std::endl;
            break;
        default:
            std::cout << "未知错误!" << std::endl;
            break;
        }
        std::cout<<"------------------------------"<<std::endl;
        std::cout<<"---------查看备注信息-----------"<<std::endl;
        auto remark = people.remarks();
        for(int i = 0;i<people.remarks_size();i++)
        {
            std::cout<<"备注标题"<<i+1<<": "<< ((remark.begin())->first)<<std::endl;
            std::cout<<"备注内容: "<<((remark.begin())->second)<<std::endl;
        }
        std::cout<<"----------------------------------"<<std::endl;
    }
    in.close();
    return 0;
}